/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.preRegistration.service;

import com.fzhucc.modules.preRegistration.domain.PreRegistrationConfig;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.preRegistration.repository.PreRegistrationConfigRepository;
import com.fzhucc.modules.preRegistration.domain.dto.PreRegistrationConfigDto;
import com.fzhucc.modules.preRegistration.domain.dto.PreRegistrationConfigQueryCriteria;
import com.fzhucc.modules.preRegistration.service.mapstruct.PreRegistrationConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-01-09
**/
@Service
@RequiredArgsConstructor
public class PreRegistrationConfigService {

    private final PreRegistrationConfigRepository preRegistrationConfigRepository;
    private final PreRegistrationConfigMapper preRegistrationConfigMapper;

    public Map<String,Object> queryAll(PreRegistrationConfigQueryCriteria criteria, Pageable pageable){
        Page<PreRegistrationConfig> page = preRegistrationConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(preRegistrationConfigMapper::toDto));
    }

    public List<PreRegistrationConfigDto> queryAll(PreRegistrationConfigQueryCriteria criteria){
        return preRegistrationConfigMapper.toDto(preRegistrationConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public PreRegistrationConfigDto findById(Integer id) {
        PreRegistrationConfig preRegistrationConfig = preRegistrationConfigRepository.findById(id).orElseGet(PreRegistrationConfig::new);
        ValidationUtil.isNull(preRegistrationConfig.getId(),"PreRegistrationConfig","id",id);
        return preRegistrationConfigMapper.toDto(preRegistrationConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    public PreRegistrationConfigDto create(PreRegistrationConfig resources) {
        return preRegistrationConfigMapper.toDto(preRegistrationConfigRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(PreRegistrationConfig resources) {
        PreRegistrationConfig preRegistrationConfig = preRegistrationConfigRepository.findById(resources.getId()).orElseGet(PreRegistrationConfig::new);
        ValidationUtil.isNull( preRegistrationConfig.getId(),"PreRegistrationConfig","id",resources.getId());
        preRegistrationConfig.copy(resources);
        preRegistrationConfigRepository.save(preRegistrationConfig);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            preRegistrationConfigRepository.deleteById(id);
        }
    }

    public void download(List<PreRegistrationConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PreRegistrationConfigDto preRegistrationConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("简介", preRegistrationConfig.getIntro());
            map.put("标题", preRegistrationConfig.getTitle());
            map.put("封面", preRegistrationConfig.getPic());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}